package com.niuwa.p2p.service.impl.borrowinfomg.enterpriseinfomg;

import java.util.Date;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.utils.DateUtil;
import com.niuwa.p2p.core.utils.DictConstant.CustStatus;
import com.niuwa.p2p.core.utils.RandomImageGenerator;
import com.niuwa.p2p.dao.borrowinfomg.enterpriseinfomg.IEnterpriseinfoMgDao;
import com.niuwa.p2p.dao.channel.CorpLoginDao;
import com.niuwa.p2p.entity.channel.CorpContactor;
import com.niuwa.p2p.entity.channel.CorpLogin;
import com.niuwa.p2p.service.borrowinfomg.enterpriseinfomg.ICorpLoginMgService;
import com.niuwa.p2p.service.mail.SimpleMailSenderService;
import com.niuwa.p2p.service.sequence.ISequenceService;
import com.niuwa.p2p.vo.borrowinfomg.enterpriseinfomg.CorpBasicInfoVo;

@Service
public class CorpLoginServiceMgImpl implements ICorpLoginMgService {

	@Autowired
	private CorpLoginDao corpLoginDao;
	@Autowired
	private IEnterpriseinfoMgDao enterpriseinfoMgDao;
	@Autowired
	private SimpleMailSenderService simpleMailSenderService;
	@Autowired
	private ISequenceService sequenceService;

	private static String LOGIN_URL = "http://localhost:8080/niuwa-opr/login.jsp";
	private static String SEQ_CORP_LOGIN_ID = "CPLG";
	@Override
	public void saveCorpLogin(CorpLogin cl) {
		this.corpLoginDao.insert(cl);
	}

	@Override
	public void sendEmailToCorpLogin(CorpLogin cl, String pwd) {
		String subject = "牛娃小贷用户开通";
		simpleMailSenderService.send(cl.getLoginName(), subject,
				generateEmailContent(cl, pwd));
	}

	@Override
	public void saveCorpContactor(String corpCode) throws Exception {
		CorpBasicInfoVo cbiv = this.enterpriseinfoMgDao
				.selectCorpBasicInfoByChannelCode(corpCode);
		for (CorpContactor cc : cbiv.getCorpContactor()) {
			if(!checkExistLoginName(cc.getEmail())){
				String pwd = RandomImageGenerator.randomString(8);
				CorpLogin cl = new CorpLogin();
				Long corpId = sequenceService.getNextSequenceBySeqKey(SEQ_CORP_LOGIN_ID);
				cl.setId(corpId);
				cl.setCorpCode(corpCode);
				cl.setLoginName(cc.getEmail());
				cl.setPassword(DigestUtils.md5Hex(pwd));
				cl.setCreateTime(new Date());
				cl.setStatus(CustStatus.NORMAL.getCode());
				corpLoginDao.insert(cl);
				sendEmailToCorpLogin(cl, pwd);
			}
		}
	}
	
	private boolean checkExistLoginName(String loginName){
		CorpLogin cl =  corpLoginDao.selectByLoginName(loginName);
		return cl != null ? true : false;
	}

	public String generateEmailContent(CorpLogin cl, String pwd) {
		StringBuffer buf = new StringBuffer("<html><body>");
		buf.append("    尊敬的牛娃小贷用户，您好!<br><br>\n");
		buf.append("您已经于"
				+ DateUtil.formatDateToString(new Date(), "yyyy-MM-dd")
				+ "成功开通账户,");
		buf.append("账户名：" + cl.getLoginName() + "    ");
		buf.append("账户密码 : " + pwd + "<br> \n");
		buf.append("如非本人操作请致电400-8846-8988并登录牛娃小贷系统进行核实。<br>\n");
		buf.append("<a href ='" + LOGIN_URL + "'>点此登录牛娃小贷系统 </a><br>\n");
		buf.append("如果点击上述文字链接没有反应，请把下面的代码复制到浏览器地址栏中打开：<br>\n");
		buf.append(LOGIN_URL + "<br>\n");
		buf.append("--------------------------------------------------------------------------------------------------------------------------------- <br>\n");
		buf.append("此邮件由系统自动发送，请勿直接回复。<br>");
		buf.append("如有疑问，可拨打致电<b>400-8846-8988</b>进行核实。<br>");
		buf.append("感谢您的关注与支持!<br>");
		buf.append("</body></html>");
		return buf.toString();
	}
}
